<?php

namespace App\Console\Commands;

use App\Exports\OrderExport;
use App\Model\CcmallOrderItem;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class ExportOrders extends Command
{
    const FILE_NAME = 'Export.data.xlsx'; // you're default excel path

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'export:order {fileName?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'export order';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $fileName = $this->argument('fileName');

        if (null === $fileName) {
            $fileName = self::FILE_NAME;
        }

        $diffSkuArray = [
            'i-3c28e75c', 'i-e143d1e7', 'i-f49a5b67', 'i-369b7e48', 'i-32ab5f32', 'i-2289ab50', 'i-66ad1f7f', 'i-36e65ce5', 'i-3a0938b1'
        ];

        $items = CcmallOrderItem::query()->whereIn('commodity_code', $diffSkuArray)->get(['order_code as code'])->toArray();

        if (is_array($items)) { 
            $codes = [];
            foreach ($items as $k => $v) {
                $codes[] = $v['code'];
            }

            Excel::store(new OrderExport($codes), $fileName);
            $this->output->writeln("export oder success");
        } else {
            $this->output->error('error');
        }


    }
}
